<?php
declare (strict_types=1);

namespace app\user\service\user\admin;

use app\common\logic\category\admin\Category;
use app\common\service\BaseService;
use app\course\logic\base\admin\CourseStudent;
use app\course\logic\classes\admin\Classes as ClassesLogic;
use app\course\logic\live\admin\Live as LiveLogic;
use app\course\logic\live\admin\LiveSmall as LiveSmallLogic;
use app\course\logic\video\admin\Video as VideoLogic;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * Class Course
 *
 * @package app\user\service\user\admin
 */
class Course extends BaseService
{

    /**
     * @Inject()
     *
     * @var CourseStudent
     */
    private $courseStudent;

    /**
     * 学员课程
     *
     * @param int $userId
     * @return \eduline\admin\page\interfaces\think\Response
     */
    public function studentCourse(int $userId)
    {
        $map = [];
        if (input('?get.course_title')) {
            $title = trim(input('get.course_title/s'), ' ');
            $title && $map[] = ['course.title', 'like', '%' . $title . '%'];
        }

        $data = $this->courseStudent->getCourseListByUserId($userId);
        // 定义字段
        $keyList = [
            'title'        => ListItem::make('custom')->title('课程名'),
            'course_type'  => ListItem::make('custom')->title('课程类型'),
            'study_status' => ListItem::make('custom')->title('学习状态'),
            'learn_rate'   => ListItem::make()->title('学习进度'),
            'total_time'   => ListItem::make()->title('学习时长'),
            'lately_time'  => ListItem::make()->title('最近学习时间'),
            'from_text'    => ListItem::make()->title('加入方式'),
            'create_time'  => ListItem::make('custom')->title('加入时间')
        ];

        $searchKey = [
            // 'course_title' => FormItem::make('input')->title(false)->placeholder('课程名')->maxlength(30),
        ];

        // 设置表单
        $list = new PageList;
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 获取课程列表
     *
     * @param int $userId
     * @return \eduline\admin\page\interfaces\think\Response
     */
    public function getCourseList(int $userId)
    {
        $mhmId = Request::get('mhm_id/d', request()->mhm_id);

        $map = [
            // ['mhm_id', '=', Request::get('mhm_id', request()->mhm_id)],
            // ['solo', '=', 0]
        ];
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        $category = Request::get('category/d', 0);
        if ($category && $mhmId === 1) {
            $map[] = ['category', 'like', '%,' . $category . ',%'];
            $map[] = ['mhm_id', '=', $mhmId];
        } else if ($category) {
            $map[] = ['mhm_category', 'like', '%,' . $category . ',%'];
        }
        $count = Request::get('count/d', 15);
        $type  = Request::get('type/d', 1);
        $data  = [];
        // $ids   = app(ClassesLinkLogic::class)->where('classes_id', $classes_id)->column('course_id');
        // $ids && $map[] = ['id', 'not in', $ids];

        switch ($type) {
            case 2:
                $mod = app(LiveLogic::class);
                break;
            case 3:
                $mod = app(LiveSmallLogic::class);
                break;
            case 4:
                $mod = app(ClassesLogic::class);
                break;
            case 1:
            default:
                $mod = app(VideoLogic::class);
                break;
        }

        $data = $mod->with(['cover_url'])
            ->where($map)
            ->where('id', 'not in', function ($query) use ($userId) {
                $query->table(env('database.prefix') . 'course_student')
                    ->where('user_id', $userId)
                    ->where('expire_time', '>', Request::time())
                    ->field('course_id');
            })
            ->field(['id', 'title', 'cover', 'scribing_price', 'price'])
            ->paginate($count);

        // 设置表单
        $list        = new PageList();
        $list->datas = $data;
        $list->types = [
            ['id' => 1, 'title' => '点播课'],
            ['id' => 2, 'title' => '大班课'],
            ['id' => 3, 'title' => '小班课'],
            ['id' => 4, 'title' => '班级课'],
        ];

        return $list->send();

    }

    /**
     * 获取课程分类列表
     *
     * @param int $pid
     * @return \think\Response
     */
    public function getCourseCategory(int $pid)
    {
        $cates = app(Category::class)->getList($pid)->visible(['id', 'title', 'hasChildren'])->toArray();

        return $this->parseToData($cates, 1, '获取成功');
    }

    /**
     * 获取课程分类树形结构
     *
     * @return \think\Response
     */
    public function getCourseCategoryCascader()
    {
        $cates = app(Category::class)->cascader();

        return $this->parseToData($cates, 1, '获取成功');
    }

    /**
     * 给学员添加课程
     *
     * @param int $userId
     * @return \think\Response
     * @throws \Exception
     */
    public function addCourse(int $userId)
    {
        $courseIds = Request::post('course_ids/a', []);

        $this->courseStudent->addCourse($userId, $courseIds);

        return $this->parseToData([], 1, '添加课程成功');
    }
}